package Configuration;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Checks for validity of the received messages and formats the outgoing messages
 * 
 * @author Shaowei
 *
 */
public class Protocol {

	private static String[] serverKeywords = { "CON", "SET", "POW" };
	private static String[] meterKeywords = { "CON", "OUT", "STP","ON","OFF" };

	public static boolean verify(String message) {
		String[] date = message.split(" ");
		
		if (date[0].equals(serverKeywords[0])
				&& Pattern.matches("\\d*", date[1]) && date.length == 4) {
			if (verifyDate(date[2] + " " + date[3])) {
				return true;
			}
			return false;
		}
		
		String[] temp = new String[6];
		temp[0] = "CON \\d* All";
		temp[1] = "SET \\d* POW off";
		temp[2] = "SET \\d* POW on";
		temp[3] = "OUT \\d* true";
		temp[4] = "OUT \\d* false";
		temp[5] = "STP \\d*";

		String[] splitms = message.split("\\$");
		String str = splitms[0];
		for (String pattern : temp) {
			if (Pattern.matches(pattern, str)) {
				return true;
			}
		}
		return false;
	}

	private static boolean verifyDate(String message) {
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm");
		Date testDate = null;
		try {
			testDate = sdf.parse(message);
			return true;
		} catch (ParseException e) {
			return false;
		}
	}
	
	/**
	 * Formats one consumption data
	 * @param meterID
	 * @param timeStamp
	 * @param consumptionData
	 * @return
	 */
	public static String formatSingleData(String meterID, String timeStamp,
			Double consumptionData) {

		String ts = timeStamp.replaceAll("$", "");
		String singleData = serverKeywords[0] + " " + meterID + " " + ts + " "
				+ consumptionData;
		return singleData;
	}
	
	/**
	 * Formats all the voltage values stored in the map
	 * @param meterID
	 * @param consumptionData
	 * @return
	 */
	public static String formatAllData(String meterID, Map<String, Double> consumptionData) {
		String data = "";
		for (Map.Entry<String, Double> entry : consumptionData.entrySet()) {
			data += serverKeywords[0] + " " + meterID + " " + entry.getKey()
					+ " " + entry.getValue() + ":";
		}
		return data;
	}
	
	
	/**
	 * Formats the outage message
	 * @param meterID
	 * @return
	 */
	public static String formatOutageMessage(String meterID) {
		String outageMsg = meterKeywords[1] + " " + meterID;
		return outageMsg;
	}
	
	/**
	 * Formats the meter stoppping message
	 * @param meterID
	 * @return
	 */
	public static String formatStoppingMessage(String meterID) {
		String stoppingMsg = meterKeywords[2] + " " + meterID;
		return stoppingMsg;
	}
	
	/**
	 * Formats the meter power cutting off message
	 * @param meterID
	 * @return
	 */
	public static String formatPoweroffMessage(String meterID) {
		String msg = meterKeywords[4] + " " + meterID;
		return msg;
	}
	
	/**
	 * Formats the meter power on message after a cut-off message
	 * @param meterID
	 * @return
	 */
	public static String formatPowerOnMessage(String meterID) {
		String msg = meterKeywords[3] + " " + meterID;
		return msg;
	}
}
